package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcAllocationService;
import com.vtuzx.kingdee.xqc_api.service.IXqcStockTakeService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 调拨
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/allocation")
@RestController
public class XqcAllocationController {

    @Autowired
    IXqcAllocationService allocationService;

    /**
     * 查询仓位
     */
    @PostMapping("/get_locationNo")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
    } )
    public Object getLocationNoList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getLocationNoList(_userId, name, param);
    }

    /**
     * 根据RFID获取物料
     */
    @PostMapping("/get_user_allocation_rfid")
    @VtuzxApi
    public Object getUserAllocationRfid(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        List<String> rfidList = VtuzxUtil.getObject(param, "rfid");
        return allocationService.getUserAllocationRfid(rfidList);
    }

    /**
     * 调拨扫描箱子(批量)
     */
    @PostMapping("/get_user_allocation_box_list")
    @VtuzxApi
    public Object getUserAllocationBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getUserAllocationBoxList(_userId, name, param);
    }

    /**
     * 调拨添加箱子
     */
    @PostMapping("/add_user_allocation_box")
    @VtuzxApi
    public Object addUserAllocationBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.addUserAllocationBox(_userId, name, param);
        return null;
    }

    /**
     * 调拨添加箱子批量
     */
    @PostMapping("/add_user_allocation_box_list")
    @VtuzxApi
    public Object addUserPurInMaterialBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.addUserPurInMaterialBoxList(_userId, name, param);
        return null;
    }

    /**
     * 调拨完成
     */
    @PostMapping("/save_user_allocation")
    @VtuzxApi
    public Object saveUserAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.saveUserAllocation(_userId, name, param);
    }


    /**
     * 调拨审核
     */
    @PostMapping("/audit_user_allocation")
    @VtuzxApi
    public Object auditUserAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.auditUserAllocation(_userId, name, param);
        return null;
    }

    /**
     * 调拨反审
     */
    @PostMapping("/unaudit_user_allocation")
    @VtuzxApi
    public Object unauditUserAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.unauditUserAllocation(_userId, name, param);
        return null;
    }


    /**
     * 删除调拨
     */
    @PostMapping("/delete_allocation")
    @VtuzxApi
    public Object deleteAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.deleteAllocation(_userId, name, param);
        return null;
    }



    /**
     * 删除物料
     */
    @PostMapping("/delete_material_allocation")
    @VtuzxApi
    public Object deleteMaterialAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.deleteMaterialAllocation(_userId, name, param);
        return null;
    }


    /**
     * 删除箱子
     */
    @PostMapping("/delete_box_allocation")
    @VtuzxApi
    public Object deleteBoxAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.deleteBoxAllocation(_userId, name, param);
        return null;
    }

    /**
     * 查询调拨任务列表
     */
    @PostMapping("/get_user_task")
    @VtuzxApi
    public Object getUserTask(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getUserTask(_userId, name, param);
    }


    /**
     * 获取临时加到数据库的物料信息
     */
    @PostMapping("/get_user_material")
    @VtuzxApi
    public Object getUserMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getUserMaterial(_userId, name, param);
    }

    /**
     * 获取临时加到数据库的物料箱子信息
     */
    @PostMapping("/get_user_material_box")
    @VtuzxApi
    public Object getUserMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getUserMaterialBox(_userId, name, param);
    }

    /**
     * 查询调拨履历列表
     */
    @PostMapping("/get_allocation_history_list")
    @VtuzxApi
    public Object getAllocationList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getAllocationList(_userId, name, param);
    }

    /**
     * 查询调拨履历详细
     */
    @PostMapping("/get_allocation_history_detail")
    @VtuzxApi
    public Object getAllocationHistoryDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getAllocationHistoryDetail(_userId, name, param);
    }

    /**
     * 查询调拨履历详细箱号
     */
    @PostMapping("/get_allocation_history_detail_box")
    @VtuzxApi
    public Object getAllocationHistoryDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getAllocationHistoryDetailBox(_userId, name, param);

    }

    /**
     * 从即时库存里获取箱子
     */
    @PostMapping("/get_material_box_from_stock")
    @VtuzxApi
    public Object getMaterialBoxFromStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getMaterialBoxFromStock(_userId, name, param);

    }

    /**
     * 查询调拨详细
     */
    @PostMapping("/get_allocation_web_detail")
    @VtuzxApi
    public Object getAllocationWebDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getAllocationWebDetail(_userId, name, param);

    }

    /**
     * 查询调拨详细箱号
     */
    @PostMapping("/get_allocation_web_detail_box")
    @VtuzxApi
    public Object getAllocationWebDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getAllocationWebDetailBox(_userId, name, param);
    }


    /**
     * 更新网页调拨仓库仓位
     */
    @PostMapping("/update_allocation")
    @VtuzxApi
    public Object updateAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.updateAllocation(_userId, name, param);
        return null;
    }


    /**
     * 更新网页调拨仓库仓位（全部）
     */
    @PostMapping("/update_all_allocation")
    @VtuzxApi
    public Object updateAllAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.updateAllAllocation(_userId, name, param);
        return null;
    }

    /**
     * 任务下发到pda
     */
    @PostMapping("/issue_user_allocation")
    @VtuzxApi
    public Object issueUserAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.issueUserAllocation(_userId, name, param);
        return null;
    }

    /**
     * 任务下发到pda（反审）
     */
    @PostMapping("/unissue_user_allocation")
    @VtuzxApi
    public Object unissueUserAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.unissueUserAllocation(_userId, name, param);
        return null;
    }



    /**
     * 获取方案下层物料
     */
    @PostMapping("/get_allocation_task_detail")
    @VtuzxApi
    public Object getAllocationTaskDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getAllocationTaskDetail(_userId, name, param);
    }

    /**
     * 获取方案下层物料箱子
     */
    @PostMapping("/get_allocation_task_detail_box")
    @VtuzxApi
    public Object getAllocationTaskDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getAllocationTaskDetailBox(_userId, name, param);
    }

    /**
     * pda添加任务中的箱号
     */
    @PostMapping("/add_task_allocation_box")
    @VtuzxApi
    public Object addTaskAllocationBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.addTaskAllocationBox(_userId, name, param);
        return null;
    }


    /**
     * pda添加任务中的箱号
     */
    @PostMapping("/rfid_add_task_allocation_box")
    @VtuzxApi
    public Object rfidAddTaskAllocationBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.rfidAddTaskAllocationBox(_userId, name, param);
        return null;
    }


    /**
     * 扫描pda任务箱子
     */
    @PostMapping("/get_task_allocation_box")
    @VtuzxApi
    public Object getTaskAllocationBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getTaskAllocationBox(_userId, name, param);

    }

    /**
     * 扫描pda任务箱子(扫描rfid)
     */
    @PostMapping("/get_task_allocation_box_rfid")
    @VtuzxApi
    public Object getTaskAllocationBoxRfid(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getTaskAllocationBoxRfid(_userId, name, param);

    }


    /**
     * 销售订单调拨查询临时物料数
     */
    @PostMapping("/get_sale_allocation_add_material_count")
    @VtuzxApi
    public Object getSaleAllocationAddMaterialCount(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getSaleAllocationAddMaterialCount(_userId, name, param);

    }


    /**
     * 销售订单调拨查询临时箱子
     */
    @PostMapping("/get_sale_allocation_add_material_box")
    @VtuzxApi
    public Object getSaleAllocationAddMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return allocationService.getSaleAllocationAddMaterialBox(_userId, name, param);
    }


    /**
     * 删除销售订单调拨临时箱子
     */
    @PostMapping("/delete_sale_allocation_add_material_box")
    @VtuzxApi
    public Object deleteSaleAllocationAddMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        allocationService.deleteSaleAllocationAddMaterialBox(_userId, name, param);
        return null;
    }

    /**
     * 导出pda调拨单
     */
    @PostMapping("/download_pda_allocation")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadPdaAllocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return allocationService.downloadPdaAllocation(token, param);
    }

}
